package com.exartisan.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.exartisan.bean.FbComment;
import com.exartisan.bean.Post;
import com.exartisan.network.NetworkUtility;
import com.exartisan.thanhdoanhn.R;
import com.exartisan.util.DateTimeFormat;
import com.exartisan.util.Util;
import com.loopj.android.image.SmartImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

public class AdapterDetailNormal extends BaseAdapter {

	Context context;
	Post post;
	ArrayList<FbComment> fbComments;
	boolean isMoreDetail;
	String wvURL;

	public AdapterDetailNormal(Context context, Post post,
			ArrayList<FbComment> fbComments, boolean isMoreDetail, String wvURL) {
		this.context = context;
		this.post = post;
		this.fbComments = fbComments;
		this.isMoreDetail = isMoreDetail;
		this.wvURL = wvURL;
	}

	@Override
	public int getCount() {
		if(isMoreDetail)
			return fbComments.size()+2;
		else return fbComments.size()+1;
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View listView;

		listView = new View(context);
		switch (getItemViewType(position)) {
		case 0:
			if (convertView == null) {
				// declare
				listView = inflater.inflate(R.layout.activity_detail_normal,
						null);
				ImageView imgCover = (ImageView) listView
						.findViewById(R.id.imgDescription);
				TextView tvTitle = (TextView) listView
						.findViewById(R.id.tvTitle);
				TextView tvDesc = (TextView) listView
						.findViewById(R.id.detail_normal);
				TextView tvViews = (TextView) listView.findViewById(R.id.tvViews); 
				TextView tvLikes = (TextView) listView.findViewById(R.id.tvLikesBlog);
				TextView tvComments = (TextView) listView.findViewById(R.id.tvComments);
				

				// fill data
				//imgCover.setImageUrl(post.getImageURL());
				
				ImageLoader.getInstance().displayImage(post.getImageURL(), imgCover);
				tvTitle.setText(post.getTitle());
				tvDesc.setText(post.getDescription());
				tvViews.setText(post.getViews() + 1 + "");
				Util.setLikeCount(post.getFbFeedID(), tvLikes);
				Util.setCommentCount(post.getFbFeedID(), tvComments);
				
			} else {
				listView = (View) convertView;
				TextView tvLikes = (TextView) listView.findViewById(R.id.tvLikesBlog);
				TextView tvComments = (TextView) listView.findViewById(R.id.tvComments);
				Util.setLikeCount(post.getFbFeedID(), tvLikes);
				Util.setCommentCount(post.getFbFeedID(), tvComments);
			}
			break;
		case 1:
			if (convertView == null) {
				// declare
				listView = inflater.inflate(R.layout.webview_normal, null);
				final WebView webView = (WebView) listView
						.findViewById(R.id.web_normal);
				WebSettings webSettings = webView.getSettings();
				webSettings.setJavaScriptEnabled(true);
				webView.loadUrl(wvURL);
		        final ProgressBar prBar = (ProgressBar) listView.findViewById(R.id.pro_webblog);
		        final LinearLayout l = (LinearLayout) listView.findViewById(R.id.linner_wv);
				webView.setWebViewClient(new WebViewClient(){
					@Override
					public void onPageFinished(WebView view, String url) {
						super.onPageFinished(view, url);
						l.setVisibility(View.VISIBLE);
						prBar.setVisibility(View.GONE);
					}
				});
			} else {
				listView = (View) convertView;
			}
			break;
		default:
			CommentViewHolder holder = new CommentViewHolder();
			if (convertView == null) {
				holder = new CommentViewHolder();
				listView = inflater.inflate(R.layout.activity_list_normal, null);
				holder.imgAvatar = (SmartImageView) listView.findViewById(R.id.imgUser);
				holder.tvUserName = (TextView) listView
						.findViewById(R.id.txtUser_normal);
				holder.tvCreatedDate = (TextView) listView
						.findViewById(R.id.txtDate_normal);
				holder.tvComment = (TextView) listView
						.findViewById(R.id.txtcomment_normal);
				listView.setTag(holder);
				
			} else {
				listView = (View) convertView;
				holder = (CommentViewHolder) listView.getTag();
			}
			int posTemp = 0;
			if(isMoreDetail== true) posTemp = position - 2;
			else posTemp = position - 1;
			
			//holder.imgAvatar.setImageUrl(NetworkUtility.GRAPH_API_URL + "/" + fbComments.get(posTemp).getFbUser().getId() + "/picture?width=100&height=100");
			
			ImageLoader.getInstance().displayImage(NetworkUtility.GRAPH_API_URL + "/" + fbComments.get(posTemp).getFbUser().getId() + "/picture?width=100&height=100", holder.imgAvatar);
			holder.tvUserName.setText(fbComments.get(posTemp).getFbUser()
					.getName());
			holder.tvCreatedDate.setText(DateTimeFormat.format(fbComments.get(posTemp)
					.getCreated_time(), DateTimeFormat.SHORT_VALUE));
			holder.tvComment.setText(fbComments.get(posTemp).getMessage());
			break;
		}
		 
		return listView;
	}
	
	@Override
	public int getViewTypeCount() {
	    return 3;
	}

	
	@Override
	public int getItemViewType(int position) {

		if (position == 0)
			return 0;
		else if (position == 1 && isMoreDetail == true)
			return 1;
		else
			return 2;
	}
	
	public void setComments(ArrayList<FbComment> comments){
		this.fbComments = comments;
	}
	
	private class CommentViewHolder{
		SmartImageView imgAvatar;
		TextView tvUserName, tvCreatedDate, tvComment;
	}

}
